package org.example.controller;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.model.BlogInfo;
import org.example.model.Constant;
import org.example.model.Result;
import org.example.service.BlogService;
import org.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 获取博客列表
     * @return
     */
    @GetMapping("/getBlogList")
    public List<BlogInfo> getBlogList() {
        log.info("获取博客列表");
        return blogService.getBlogList(); // 根据前面配置好的进行统一结果返回
    }

    /**
     * 获取当前ID的文章信息
     * @param blogId
     * @param request
     * @return
     */
    @GetMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("获取博客id为 {} 的文章信息", blogId);
        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        Integer id = JwtUtils.getUserIdFromToken(token);
        if (blogInfo != null && blogInfo.getUserId() == id) {
            log.info("LoginUser: true");
            blogInfo.setLoginUser(true);
        }
        log.info("blogInfo: {}", blogInfo);
        return blogInfo;
    }

    /**
     * 添加文章
     * @param blogInfo
     * @param request
     * @return
     */
    @PostMapping("/addBlog")
    public Result addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("添加文章, blogInfo: {} ", blogInfo);

        // 校验参数
        if (!StringUtils.hasLength(blogInfo.getContent())) {
            log.info("添加失败，内容不能为空");
            return Result.fail(-1, "内容不能为空");
        }
        if (!StringUtils.hasLength(blogInfo.getTitle())) {
            log.info("添加失败，标题不能为空");
            return Result.fail(-1, "标题不能为空");
        }
        // 校验token
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getUserIdFromToken(token);
        if (userId == null) {
            log.info("添加失败，用户未登录");
            return Result.unlogin();
        }
        blogInfo.setUserId(userId);
        Integer update = blogService.addBlog(blogInfo);
        if (update == 0) {
            return Result.fail(-1, "添加失败");
        }
        log.info("添加成功");
        return Result.success("添加成功");
    }

    /**
     * 修改文章
     */
    @PostMapping("/update")
    public Result update(@RequestBody BlogInfo blogInfo) {
        log.info("修改文章信息：{}", blogInfo);
        Boolean result = blogService.update(blogInfo);
        log.info("result: {}", result);
        return Result.success(result);
    }

    /**
     * 删除文章
     */

    @PostMapping("/delete")
    public Result delete(Integer blogId) {
        log.info("删除id为: {} 的文章", blogId);
        blogService.deleteBlogById(blogId);
        return Result.success("删除成功");
    }
}
